package cn.doitedu.serde;

import java.io.*;
import java.lang.reflect.Field;

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);


        Person p = new Person("张散散", 175.5, 18);

        dout.writeUTF(p.getClass().getName());
        dout.writeUTF(p.getName());
        dout.writeDouble(p.getHeight());
        dout.writeInt(p.getAge());

        byte[] bytes = bout.toByteArray();

        // ---------------------


        ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
        DataInputStream dIn = new DataInputStream(bIn);

        String className = dIn.readUTF();
        String pName = dIn.readUTF();
        double pHeight = dIn.readDouble();
        int pAge = dIn.readInt();

        Class<?> aClass = Class.forName(className);
        Object o = aClass.newInstance();

        Field name = aClass.getDeclaredField("name");
        name.setAccessible(true);
        name.set(o,pName);





    }
}
